package com.xinzhi.savingpot.userinfo.service.impl;

import com.xinzhi.savingpot.userinfo.entity.Problem;
import com.xinzhi.savingpot.userinfo.mapper.IHistoricalProblemMapper;
import com.xinzhi.savingpot.userinfo.service.IHistoricalProblemService;
import com.xinzhi.savingpot.util.BaseResponse;
import com.xinzhi.savingpot.util.IsMobileUtil;
import com.xinzhi.savingpot.vo.HistoricalProblemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 未解决
 */
@Service
public class HistoricalProblemServiceImpl implements IHistoricalProblemService {

    @Autowired
    private IHistoricalProblemMapper iHistoricalProblemMapper;
    @Autowired
    private IsMobileUtil isMobileUtil;


    @Override
    public BaseResponse add(HistoricalProblemVo historicalProblemVo) {

        Problem problem = new Problem();
        if (historicalProblemVo != null) {
            if (isMobileUtil.checkCellphone(historicalProblemVo.getCyphone())) {
                problem.setUserNumber(historicalProblemVo.getUserNumber());
                problem.setUploadtime(new Date());
                problem.setCyphone(historicalProblemVo.getCyphone());
                problem.setMeetproblem(historicalProblemVo.getMeetproblem());
                problem.setProblem_description(historicalProblemVo.getProblem_description());
                iHistoricalProblemMapper.insert(problem);
                return new BaseResponse(1, "提交成功");

            }
            return new BaseResponse(2, "请输入正确的手机号");


        }
        return new BaseResponse(-1, "所遇问题,问题描述,手机号,QQ不能为空");


    }

    @Override
    public BaseResponse chaxun(int userNumber) {
        List<Problem> xi = iHistoricalProblemMapper.select(userNumber);

        if (xi.isEmpty()){
            return new BaseResponse(400,"查询失败");
        }
        HistoricalProblemVo historicalProblemVo=null;
        List<HistoricalProblemVo> list= new ArrayList<>();
        for (Problem pl:xi){
            historicalProblemVo = new HistoricalProblemVo();
            historicalProblemVo.setProblem_description(pl.getProblem_description());
            historicalProblemVo.setUploadtime(pl.getUploadtime());
            historicalProblemVo.setQQ(pl.getQQ());
            historicalProblemVo.setPhoto(pl.getPhoto());
            historicalProblemVo.setMeetproblem(pl.getMeetproblem());
            historicalProblemVo.setCyphone(pl.getCyphone());
            list.add(historicalProblemVo);
        }

        return new BaseResponse(200,"查询成功",list);
    }

}

